package impl

import "adam2/internal/model"

type OptimizeAccountDaoImpl struct {
	*BaseDaoImpl
}

// 返回dao实现类
func GetOptimizeAccountDaoImpl() *OptimizeAccountDaoImpl {
	return &OptimizeAccountDaoImpl{GetBaseDaoImpl()}
}

// 返回表名
func (_optimizeAccountDaoImpl *OptimizeAccountDaoImpl) FindTableName() string {
	return "OPTIMIZE_ACCOUNT"
}

// 清空表
func (_optimizeAccountDaoImpl *OptimizeAccountDaoImpl) TruncateTable() {
	_optimizeAccountDaoImpl.db.Exec("truncate table optimize_account")
}

// 根据日期，从optimize_account_log表中向optimize_account表中插入记录
func (_optimizeAccountDaoImpl *OptimizeAccountDaoImpl) InsertFromOptimizeAccountLogByDate(date string) {
	_optimizeAccountDaoImpl.db.Exec("insert into optimize_account(account_name, hold_stock_number, stock_assets, capital_assets, total_assets, total_stamp_duty, total_registrate_fee_when_buy, total_commission_when_buy, total_registrate_fee_when_sell, total_commission_when_sell) "+
		"select account_name, hold_stock_number, stock_assets, capital_assets, total_assets, total_stamp_duty, total_registrate_fee_when_buy, total_commission_when_buy, total_registrate_fee_when_sell, total_commission_when_sell "+
		"from optimize_account_log where date_=to_date(?,'yyyy-mm-dd')", date)
}

// 根据日期删除记录
func (_optimizeAccountDaoImpl *OptimizeAccountDaoImpl) DeleteByDate(date string) {
	_optimizeAccountDaoImpl.db.Exec("delete optimize_account where date_ = to_date(?,'yyyy-mm-dd')", date)
}

// 查询所有记录
func (_optimizeAccountDaoImpl *OptimizeAccountDaoImpl) FindAll() model.OptimizeAccountArray {
	var optimizeAccountArray model.OptimizeAccountArray
	_optimizeAccountDaoImpl.db.Raw("select * from optimize_account").Find(&optimizeAccountArray)
	return optimizeAccountArray
}
